package com.baizhi.cmfz.service.impl;

import com.baizhi.cmfz.dao.AdminDao;
import com.baizhi.cmfz.entity.Admin;
import com.baizhi.cmfz.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired(required = false)
    private AdminDao adminDao;
    @Autowired(required = false)
    private HttpSession session;
    @Override
    public boolean login(String username, String password,String code) {
        String realCode = (String) session.getAttribute("realCode");
        System.out.println(realCode+"===="+code);
        if (realCode.equalsIgnoreCase(code)) {
            //验证码正确，在进行用户名密码的判断
            Admin admin = adminDao.selectAdmin(username,password);
            if (admin != null) {
                //登录成功
                session.setAttribute("admin",admin);
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }
    }

    /**
     * 需要根据前台框架定义好的key值进行map封装
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Map showPageAdmin(int page, int limit) {
        int offset = (page-1)*limit;
        //查询当前页数据
        List<Admin> admins = adminDao.showPageAdmin(offset,limit);
        System.out.println("展示数据"+admins);
        //查询出总条数
        int count = adminDao.selectAdminCount();
        //封装map
        Map map = new HashMap(6);
        map.put("code",0);
        map.put("msg","");
        map.put("data",admins);
        map.put("count",count);
        return map;
    }

    @Override
    @Transactional
    public Map addAdmin(Admin admin) {
        int i = adminDao.addAdmin(admin);
        Map map = new HashMap(2);
        if (i == 1) {
            map.put("isAdd",true);
        }else {
            map.put("isAdd",false);
        }
        return map;
    }

    @Override
    @Transactional
    public Map updateAdmin(Admin admin) {
        int i = adminDao.updateAdmin(admin);
        Map map = new HashMap(2);
        if (i == 1) {
            map.put("isUpdate",true);
        }else {
            map.put("isUpdate",false);
        }
        return map;
    }

    @Override
    @Transactional
    public Map deleteAdmin(int id) {
        int i = adminDao.deleteAdmin(id);
        Map map = new HashMap(2);
        if (i == 1) {
            map.put("isDelete",true);
        }else {
            map.put("isDelete",false);
            System.out.println("测试合并分支");
        }
        return map;
    }

    @Override
    @Transactional
    public Admin selectOneAdmin(String username) {
        return adminDao.selectOneAdmin(username);
    }

    @Override
    @Transactional
    public Set<String> selectAdminRole(String username) {
        Set<String> roles = adminDao.selectAdminRole(username);
        return roles;
    }

    @Override
    @Transactional
    public Set<String> selectAdminPermision(String username) {
        Set<String> permisions = adminDao.selectAdminPermision(username);
        System.out.println("在测试分支添加代码");
        return permisions;
    }
}
